//保存表格的实例对象
var tableI = null;

function aa(){
    tableI.reload();
}

layui.use(['table','form'], function () {
    var table = layui.table;
    var form = layui.form;

    //监听顶部重置按钮
    form.on('submit(cz)',function (d){
        $("[name=commodityName]").val(undefined)
        $("[name=classifyTitle]").val(undefined)
        $("[name=commodityState]").val(undefined)
        $("input").val('');

        tableI.reload({
            url:'/personalProductList/getPersonalProductListSee',
        },true)
        return false
    })

    // 展示表格
    tableI = table.render({
        elem: '#PersonalProductList',
        url: '/personalProductList/getPersonalProductListSee',
        id: 'tableIn',
        page: {
            //       总条数  左箭头  显示的页数 右箭头  // 每页显示条数
            layout: ['count', 'prev', 'page', 'next', 'limit'],
            groups: 1,
            // 每页显示条数
            limit: 5,
            // 每页显示条数
            limits: [5,8,10]
        },
        cols: [[
            {field: '', title: '', type: 'checkbox',height:30},
            {field: 'commodityCode', title: "商品编号", width: 120,sort: true},
            {field: 'commodityImage', title: "商品图片", width: 130, templet: function (d) {
                    return '<img src="http://43.143.176.39/' + d.commodityImage + '" width="80px" height="80px">';
                }},
            {field: 'commodityName', title: "商品名称", width: 150},
            {field: 'currentPrice', title: "商品价格", width: 120,sort: true,align: 'center',templet:function (d){
                    return d.currentPrice + '￥';
                }},
            {field: 'commodityState', title: "上架/下架", width: 130, templet: function (d) {
                    var state = '<a lay-event = "sp">';
                    if (d.commodityState == 1) {
                        state += '<input type="checkbox" name="commodityState" checked lay-skin="switch" lay-text="上架|下架">';
                    }if(d.commodityState == 2) {
                        state += '<input type="checkbox" name="commodityState" lay-skin="switch" lay-text="上架|下架">';
                    }
                    state += '</a>';
                    return state;
                },
                width: 130,
            },
            {field: 'classifyTitle', title: "商品分类", width: 150},
            {field: 'commodityLabel', title: "商品标签", width: 120},
            {field: 'commodityExamine', title: "审核状态", width: 120, templet: function (d) {
                    var stt = '<a lay-event = "shh">';
                    if (d.commodityExamine == 1){
                        stt = "审核通过"+'<br>'+'<button type="submit" lay-event="shh"' +
                            ' id="auditDetails" class="layui-btn layui-btn-primary layui-btn-xs spbut">审核详情</button>';
                    } if (d.commodityExamine == 2){
                        stt = "待审核"+'<br>'+'<button type="submit" lay-event="shh"' +
                            ' id="auditDetails" class="layui-btn layui-btn-primary layui-btn-xs spbut">审核详情</button>';
                    } if (d.commodityExamine == 0){
                        stt = "审核未通过"+'<br>'+'<button type="submit" lay-event="shh"' +
                            ' id="auditDetails" class="layui-btn layui-btn-primary layui-btn-xs spbut">审核详情</button>';
                    }
                    stt += '</a>';
                    return stt;
                }},
            {title: '操作', toolbar: '#toolbtn', width: 130}
        ]],
        parseData: function (res) {
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.count,
                "data": res.data
            };
        },
        toolbar: '#toolbarbtn'
    })

    //查询 重新渲染表格
    form.on('submit(ss)', function (d) {
        d=d.field;
        console.log(d)
        table.reload('tableIn',{
            url:'/personalProductList/getPersonalProductListLike',
            where: {
                commodityName: d.commodityName,
                classifyTitle: d.classifyTitle,
                commodityLabel: d.commodityLabel
            },
        })
    })

    // 查看
    table.on('tool(test)', function (obj) {

        // 子页面向父页面传值，封装键值对，父页面拿出
        localStorage.setItem("qq",obj.data.commodityId);
        console.log(obj.data.commodityId)

        data = obj.data
        console.log(obj)
        if (obj.event === 'see') {
            layer.open({
                type: 2,
                title: '',
                content: '../goods/see.html',
                shade:[0.5,'#AFEEEE'],
                anim:4,
                area: ['80%', '80%'],
                success: function (layero, index) {
                    var body = layer.getChildFrame('body', index);
                    // 商品名称
                    body.find('#commodityName').text(obj.data.commodityName);
                    // 商品分类名称
                    body.find('#classifyTitle').text(obj.data.classifyTitle);
                    // 商品关键字
                    body.find('#searchKeywords').text(obj.data.searchKeywords);
                    // 商品编号
                    body.find('#commodityCode').text(obj.data.commodityCode);
                    //  商品状态
                    if (obj.data.commodityState == 1){
                        body.find('#commodityState').text("已上架");
                    }else if (obj.data.commodityState == 2){
                        body.find('#commodityState').text("已下架");
                    }
                    // 商品路径
                    body.find('#commodityImage').attr('src','http://43.143.176.39/' + obj.data.commodityImage);
                    // 商品标签
                    body.find('#commodityLabel').text(obj.data.commodityLabel);
                    // 商品价格
                    body.find('#currentPrice').text(obj.data.currentPrice);
                    // 商品内容
                    body.find('#commodityDetails').text(obj.data.commodityDetails);
                    // 用户名称
                    body.find('#userName').text(obj.data.userName);
                    // 上传时间
                    body.find('#createDate').text(obj.data.createDate);
                    // 审核状态
                    if (obj.data.commodityExamine == 0){
                        body.find('#commodityExamine').text("审核未通过");
                    }else if (obj.data.commodityExamine == 1){
                        body.find('#commodityExamine').text("审核通过");
                    }else if (obj.data.commodityExamine == 2){
                        body.find('#commodityExamine').text("待审核");
                    }
                }
            })
            // 删除
        } else if (obj.event === 'delete') {
            layer.confirm('真的删除' + obj.data.commodityName + '吗?', function (index) {
                $.ajax({
                    type: 'post',
                    url: '/add/productDelete',
                    data: {"commodityId": obj.data.commodityId},
                    dataType: 'json',
                    success: function (res) {
                        layer.close(index);
                        tableI.reload();
                        if (res.start == 200){
                            layer.msg(res.msg,{time:500,icon:5})
                        }
                        else {
                            layer.msg(res.msg,{time:500,icon:6})
                        }
                    },
                    error: function (e) {
                        console.log("失败");
                        console.log(e);
                    }
                })
            });
            // 修改编辑
        } else if (obj.event === 'edit') {
            layer.open({
                type: 2,
                title: '',
                content: '../goods/edit.html',
                shade:[0.5,'#AFEEEE'],
                anim:4,
                area: ['80%', '80%'],
                success: function (layero, index) {
                    let childFrame = layer.getChildFrame('body', index);
                    // 商品编号
                    childFrame.find('input[name="commodityCode"]').val(obj.data.commodityCode);
                    // 商品id
                    childFrame.find('input[name="commodityId"]').val(obj.data.commodityId);
                    // 商品名称
                    childFrame.find('input[name="commodityName"]').val(obj.data.commodityName);
                    // 商品分类id
                    childFrame.find('input[name="classifyId"]').val(obj.data.classifyId);
                    // 商品路径
                    childFrame.find('#commodityImage').attr('src','http://43.143.176.39/' + obj.data.commodityImage);
                    childFrame.find('input[name="commodityImage"]').val(obj.data.commodityImage);
                    // 商品分类名称
                    childFrame.find('input[name="classifyTitle"]').val(obj.data.classifyTitle);
                    // 商品标签
                    childFrame.find('input[name="commodityLabel"]').val(obj.data.commodityLabel);
                    // 商品关键字
                    childFrame.find('input[name="searchKeywords"]').val(obj.data.searchKeywords);
                    // 商品内容
                    childFrame.find('input[name="commodityDetails"]').val(obj.data.commodityDetails);
                    // 商品价格
                    childFrame.find('input[name="currentPrice"]').val(obj.data.currentPrice);
                    // 用户名称
                    childFrame.find('input[name="userName"]').val(obj.data.userName);
                    // 上传时间
                    childFrame.find('input[name="createDate"]').val(obj.data.createDate);
                    // 审核状态
                    if (obj.data.commodityExamine == 0){
                        childFrame.find('input[name="commodityExamine"]').val("审核未通过");
                    }else if (obj.data.commodityExamine == 1){
                        childFrame.find('input[name="commodityExamine"]').val("审核通过");
                    }else if (obj.data.commodityExamine == 2){
                        childFrame.find('input[name="commodityExamine"]').val("待审核");
                    }
                }
            })
            // 日志
        } else if (obj.event === 'journal') {
            layer.open({
                type: 2,
                title: '',
                content: '../goods/journal.html',
                shade:[0.5,'#AFEEEE'],
                anim:4,
                area: ['83%', '83%']
            })
            // 商品上下架
        } else if (obj.event === 'sp') {
            switch (data.commodityState) {
                case 1:
                    sxj(2)
                    break;
                case 2:
                    sxj(1)
                    break;
            }
            // 审核详情
        }else if(obj.event === 'shh') {
            layer.open({
                type: 2,
                title: '审核详情',
                content: '../goods/examine.html',
                shade:[0.5,'#AFEEEE'],
                anim:4,
                area: ['80%', '80%'],
            })
        }
    })

    // 上下架ajax请求修改
    function sxj(state) {
        $.ajax({
            type:"post",
            url:'/product/updateCommodity',
            data: {"commodityId":data.commodityId,"commodityState":state},
            dataType:'json',
            success:function (res) {
                if (res.start == 200){
                    layer.msg(res.msg,{time:500,icon:5})
                }
                else {
                    layer.msg(res.msg,{time:500,icon:6})
                }
                tableI.reload();
            },
            error:function(e){
                console.log("异常");
            }
        })
    }
})